<template>
  <div class="home-box">
    <!-- 顶部导航 -->
    <van-nav-bar class="page-nev-bar" fixed>
      <van-button slot="title" icon="search" type="primary" round to='/search'>搜索</van-button>
    </van-nav-bar>
    <!-- 标签页 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="item in userChannels" :key="item.id" :title="item.name">
        <ArticleList :channelId='item.id'></ArticleList>
      </van-tab>
      <!-- 右侧汉堡菜单 -->
      <div slot="nav-right" class="nav-right" @click="isChannelShow = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
      <!-- 占位 -->
      <div slot="nav-right" class="placeholder"></div>
    </van-tabs>

    <!-- 频道编辑弹出层 -->
    <van-popup v-model="isChannelShow" closeable position="bottom" :style="{ height: '90%' }"
      close-icon-position="top-left">
      <ChannelEdit :channel="userChannels" :active="active" @sendData="getEditchannelData"></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './component/article-list.vue'
import ChannelEdit from './component/channelEdit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'
export default {
  name: 'home',
  data () {
    return {
      active: 0,
      // 用户频道数据
      userChannels: [],
      // 频道列表显示
      isChannelShow: false
    }
  },
  methods: {
    // 获取用户频道数据
    async getUserChannel () {
      // 用户未登录且本地没数据
      const data = getItem('tt_channel')
      if (!this.user && data) {
        this.userChannels = data
      } else {
        // 用户登录
        try {
          const { data: res } = await getUserChannels()
          this.userChannels = res.data.channels
        } catch (err) {
          this.$toast('获取频道列表失败')
        }
      }
    },
    // 获取子组件数据的事件对象
    getEditchannelData (e) {
      this.isChannelShow = e.close
      this.active = e.active
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  filters: {},
  components: {
    ArticleList,
    ChannelEdit
  },
  created () {
    this.getUserChannel()
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
.home-box {
  padding-bottom: 100px;
  padding-top: 174px;
  .van-nav-bar {
    .van-button {
      width: 555px;
      height: 64px;
      background: rgba(255, 255, 255, 0.2);
      border: none;
    }
    /deep/ .van-icon-search {
      color: #fff;
    }
    /deep/ .van-nav-bar__title {
      max-width: unset;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      width: 100%;
      z-index: 2;
    }
    .van-tab {
      min-width: 200px;
      height: 82px;
      border-right: 1px solid #edeff3;
      font-size: 28px;
      color: #777;
    }
    // 选中样式
    .van-tab--active {
      font-size: 30px;
      color: #333;
    }
    // 下划线
    .van-tabs__line {
      width: 31px !important;
      height: 6px;
      background: rgba(50, 150, 250, 1);
      border-radius: 3px;
      bottom: 8px;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .nav-right {
      display: flex;
      justify-content: center;
      align-items: center;
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      background: rgba(255, 255, 255, 0.9);
      .toutiao-gengduo {
        font-size: 33px;
      }
      &::before {
        position: absolute;
        left: 0;
        content: '';
        width: 2px;
        height: 82px;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: 100% 100%;
      }
    }
    .placeholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
  }
}
</style>
